// pages/order/detail.js
import api from '../../utils/api'
import message from '../../utils/message'
Page({

  /**
   * 页面的初始数据
   */
  data: {

    order_detail:{},
    has_load:false,
    order_content_detail:null,
    static_url:'',
    order_no:null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var order_no = options.order_no;



    if(order_no){
      this.setData({
          order_no: order_no
      })
      this.getOrderDetail();

    }else{
      message.show_error("订单编号不能为空");
    }

  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    /*
    wx.redirectTo({
      url: '/pages/order/order',
    })
    */


    //如果是从订单创建页面 跳转过来
    /*
    wx.navigateBack({
      delta:2
    })

    */

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOrderDetail:function(  ){
    var params = {};
    params.order_no = this.data.order_no;
    var me = this;

    message.show_loading("订单加载中");
    api.getOrderDetail( params , function( resp ){
      message.hide_loading();
      if(resp.status==1){

        var detail = null;
        if( resp.data.order.detail ){
          detail = JSON.parse(resp.data.order.detail)
        }

        //var detail = JSON.parse(resp.data.order.detail)

        me.setData({
          order_detail:resp.data.order,
          has_load:true,
          order_content_detail:detail
        })

        console.log(resp.data);
      }else{
        wx.showModal({
          title: '提示',
          content: resp.message
        })
      }
    })
  },
  doOrderPay:function( e ){
    //支付订单

    if(this.data.order_detail){
      var order_no = this.data.order_detail.order_no;
      /*
      wx.navigateTo({
        url: '/pages/pay/pay?no='+order_no
      })

      */

     //var order_no = this.data.order_no;
        
     var me = this;
     api.doPay({order_no:  order_no} , function(result){
         if(result.status==1){

             //console.log(result);
             me.appletPay( result.data , order_no )
         }else{
             console.log( result.message )
         }
     });

    }
    
  },
  appletPay:function( payInfo , order_no ){
    //调用微信支付信息，勾起订单支付
    var me = this;
    wx.requestPayment({
      "timeStamp": payInfo.timeStamp.toString(),
      "nonceStr": payInfo.nonceStr,
      "package": payInfo.package,
      "signType": payInfo.signType,
      "paySign": payInfo.paySign,
      success:function( res ){
          console.log(res);
          me.getOrderDetail(order_no);
          console.log(order_no)
      },
      fail:function(res){
          console.log(res)
      }

  })

  },
  doOrderCancel:function( e ){
    //取消订单

    var order_no = this.data.order_no;
    var me = this;

    api.doOrderCancel({order_no:order_no} , function(result){

      if(result.status==1){

        /*
        wx.showToast({
          title:  result.message
        })

        */

        message.action_success("提示" , result.message , null , function(){
          me.getOrderDetail(order_no); 
        })

       
      }else{
        console.log(result)
      }

    })



  },
  doOrderComment:function( e ){
    //订单评价

    var order_no = this.data.order_detail.order_no;

    var data = {
      comment:'送货非常快',
      star:3,
      order_no:order_no
    }
    api.doOrderComment( data , function(result){

      if( result.status ==1 ){
        wx.showModal({
          title:'提示',
          content:result.message,
          success:function( res ){
            if(res.confirm){
              wx.navigateBack();
            }else{
              console.log("点击取消");
            }
          }
        })
      }

    })



  },
  doOrderComplete:function(){
    //确认订单完成

    var order_no = this.data.order_no;

    var me = this;
    

    message.confirm("是否收到货物","已收到" ,"未收到" , function (index) {
      if( index==1 ){

        var params = {};
        params['order_no'] = order_no;
        
        api.doOrderComplete( params , function(result){
          if(result.status==1){

            message.show_success( result.message  , function(){
              me.getOrderDetail();
            })
          }else{
            message.show_error( result.message );
          }
        })

      }
    })
  },
  doCopyText:function (e) {

    var text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: text,
      success:function (res) {

        wx.getClipboardData({
          success: function( res2 ){
            wx.showToast({
              title: '复制成功'
            })

          }
        })
        
      }
    })
    
  }
})